<template>
  <div>
    <el-row v-for="article in articleList" :key="article.id">
      <el-col :span="20">
        <div class="grid-content">
          <aside>
            <img :src="article.publish_img" alt="" class="article-image">
          </aside>
          <div>
            <p @click="gotoarticleDetal(article.id)">{{ article.title }}</p>
            <span class="article-content" @click="gotoarticleDetal(article.id)" v-html="article.content"></span>
            <div class="article-data">
              <span @click="addLike(article.id)" v-if="article.like_num === 0"><img src="../../assets/dianzan.png" alt="" class="img_icon">{{article.like_num}}赞</span>
              <span @click="addLike(article.id)" v-else><img src="../../assets/yidianzan.png" alt="" class="img_icon">{{article.like_num}}赞</span>
              <span @click="addCollect(article.id)" v-if="article.collect_num === 0"><img src="../../assets/shoucang.png" alt="" class="img_icon1">{{ article.collect_num }}收藏</span>
              <span @click="addCollect(article.id)" v-else><img src="../../assets/yishoucang.png" alt="" class="img_icon1">{{ article.collect_num }}收藏</span>
              <span>发布时间：{{ article.publish_time }}</span>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getarticleAPI, addLikeAPI, addCollectAPI, getarticledetailbyidAPI } from '@/api'
import { mapMutations } from 'vuex'
export default {
  name: 'muying-zx',
  data () {
    return {
      articleList: {}
    }
  },
  mounted () {
    this.getArticleList()
  },
  methods: {
    async getArticleList () {
      const { data } = await getarticleAPI()
      if (data.status !== 0) return this.$message.error(data.message)
      this.articleList = data.data
    },
    async addLike (id) {
      const { data } = await addLikeAPI(id)
      if (data.status !== 0) return this.$message.error(data.message)
      this.getArticleList()
    },
    async addCollect (id) {
      const { data } = await addCollectAPI(id)
      if (data.status !== 0) return this.$message.error(data.message)
      this.getArticleList()
    },
    ...mapMutations(['getarticledetail']),
    gotoarticleDetal (id) {
      getarticledetailbyidAPI(id).then(({ data }) => {
        this.getarticledetail(data.data)
      })
      this.$router.push('/zixundetail')
    }
  }
}
</script>

<style scoped>
  .el-row {
    margin-bottom: 20px;
  }
  .el-row:last-child {
    margin-bottom: 0;
  }
  .el-col {
    border-radius: 4px;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
    height: 100%;
    padding-left: 10px;
    display: flex;
    width: 800px;
  }
  .grid-content:hover{
    background: #fcc2d7;
  }
  aside {
    margin-right: 10px;
  }
  .article-image {
    margin-top: 10%;
    width: 140px;
    height: 112px;
  }
  .article-content {
    font-size: 10px;
    font-weight: 400;
    color: #555666;
    overflow: hidden;
    white-space: normal;
    word-break: break-word;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    opacity: .7;
    cursor:pointer;
  }
  .grid-content p {
    height: 25px;
    line-height: 30px;
    margin-top: 10px;
    margin-bottom: 10px;
    font-size: 18px;
    font-weight: 500;
    color: #222226;
    cursor: pointer;
  }
  .grid-content p:hover {
    text-decoration: underline;
  }
  .article-data {
    margin-top: 10px;
  }
  .article-data span{
    opacity:.5;
    margin: 0 3px;
    font-size: 10px;
    margin-right: 5px;
    cursor:pointer;
  }
  .img_icon {
    width: 1.5em;
    height: 1.5em;
    vertical-align:-0.35em;
    margin-right: 1px;
  }
  .img_icon1 {
    width: 2em;
    height: 2em;
    vertical-align:-0.55em;
    margin-right: 1px;
  }

</style>
